package cn.lingyangwl.agile.food.core.service.impl;

import cn.lingyangwl.agile.food.core.assembly.StockDocumentDetailAssembly;
import cn.lingyangwl.agile.food.core.manager.StockDocumentDetailManager;
import cn.lingyangwl.agile.food.core.mapper.StockDocumentDetailMapper;
import cn.lingyangwl.agile.food.model.entity.StockDocumentDetail;
import cn.lingyangwl.agile.food.model.rqrs.sku.SkuInOutDetailResp;
import cn.lingyangwl.agile.food.core.service.StockDocumentDetailService;
import cn.lingyangwl.agile.food.model.rqrs.sku.SkuInOutDetailPageReq;
import cn.lingyangwl.agile.food.model.rqrs.sku.SkuInOutDetailSaveReq;
import cn.lingyangwl.agile.food.model.rqrs.sku.SkuInOutDetailUpdateReq;
import cn.lingyangwl.framework.mybatis.plus.utils.PageUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.IService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Objects;

/**
 * 库存单据明细
 *
 * @author shenguangyang
 */
@Slf4j
@Service
public class StockDocumentDetailServiceImpl extends ServiceImpl<StockDocumentDetailMapper, StockDocumentDetail>
    implements IService<StockDocumentDetail>, StockDocumentDetailService {

    @Resource
    private StockDocumentDetailManager stockDocumentDetailManager;
    @Resource
    private StockDocumentDetailAssembly stockDocumentDetailAssembly;
    
    @Override
    public IPage<SkuInOutDetailResp> pageDetail(SkuInOutDetailPageReq req) {
        LocalDateTime beginTime = req.getBeginLocalDateTime();
        LocalDateTime endTime = req.getEndLocalDateTime();
        LambdaQueryWrapper<StockDocumentDetail> lqw = StockDocumentDetail.lqw()
            .ge(Objects.nonNull(beginTime), StockDocumentDetail::getCreatedAt, beginTime)
            .le(Objects.nonNull(endTime), StockDocumentDetail::getCreatedAt, endTime)
            .orderByDesc(StockDocumentDetail::getCreatedAt);
        IPage<StockDocumentDetail> pageData = this.page(PageUtil.to(req), lqw);
        return PageUtil.to(pageData, stockDocumentDetailAssembly::toSkuStockInOutDetailResp);
    }

    @Override
    public SkuInOutDetailResp getDetail(Long id) {
        StockDocumentDetail entity = this.getById(id);
    	return stockDocumentDetailAssembly.toSkuStockInOutDetailResp(entity);
    }

    @Override
    public boolean saveDetail(SkuInOutDetailSaveReq req) {
        StockDocumentDetail entity = stockDocumentDetailAssembly.toSkuStockInOutDetail(req);
    	return this.save(entity);
    }

    @Override
    public boolean updateDetail(SkuInOutDetailUpdateReq req) {
        StockDocumentDetail entity = stockDocumentDetailAssembly.toSkuStockInOutDetail(req);
    	return this.updateById(entity);
    }

    @Override
    public boolean deleteDetail(Long id) {
    	return this.removeById(id);
    }
    
    @Override
    public boolean batchDeleteDetail(List<Long> ids) {
    	return this.removeByIds(ids);
    }
}
